using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Toenda.Foundation.IO {
	/// <summary>
	/// Class LogSystem
	/// </summary>
	public class LogSystem {
		private StreamWriter _sw;

		// ---------------------------------------------------
		// CONSTRUCTORS
		// ---------------------------------------------------

		/// <summary>
		/// Default Ctor
		/// </summary>
		public LogSystem() {
		}

		// ---------------------------------------------------
		// PROPERTIES
		// ---------------------------------------------------

		// ---------------------------------------------------
		// EVENTS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PUBLIC MEMBERS
		// ---------------------------------------------------

		/// <summary>
		/// Open a filestream
		/// </summary>
		/// <param name="filename">The name including path of the file</param>
		/// <param name="append">True if you want to append to the file</param>
		public void OpenFile(string filename, bool append) {
			try {
				this._sw = new StreamWriter(
					filename,
					append
				);
			}
			catch(Exception ex) {
				throw new Exception(
					"FileStream Object encoured an error!",
					ex
				);
			}
		}

		/// <summary>
		/// Close the open file
		/// </summary>
		public void CloseFile() {
			try {
				this._sw.Close();
				this._sw.Dispose();
				this._sw = null;
			}
			catch(Exception ex) {
				throw new Exception(
					"FileStream Object is not initialized, please open the file first!",
					ex
				);
			}
		}

		/// <summary>
		/// Write some text to a file (only with manually open and close)
		/// </summary>
		/// <param name="text"></param>
		public void WriteToFile(string text) {
			if(this._sw == null) {
				throw new Exception("Please open a file at first!");
			}

			this._sw.WriteLine(text);
		}

		/// <summary>
		/// Write some text to a file (only with manually open and close)
		/// </summary>
		/// <param name="timestamp"></param>
		/// <param name="text"></param>
		public void WriteToFile(DateTime timestamp, string text) {
			if(this._sw == null) {
				throw new Exception("Please open a file at first!");
			}

			this._sw.WriteLine(timestamp.ToString() + ": " + text);
		}

		/// <summary>
		/// Write some text to a file
		/// </summary>
		/// <param name="filename"></param>
		/// <param name="text"></param>
		public void WriteToFile(string filename, string text) {
			StreamWriter sw = new StreamWriter(
				filename,
				true
			);

			sw.WriteLine(text);

			sw.Close();
			sw.Dispose();
		}

		/// <summary>
		/// Read some text from a file
		/// </summary>
		/// <param name="filename"></param>
		public string ReadFromFile(string filename) {
			StreamReader sr = new StreamReader(filename);

			string tmp = sr.ReadToEnd();

			sr.Close();
			sr.Dispose();

			return tmp;
		}

		// ---------------------------------------------------
		// PRIVATE MEMBERS
		// ---------------------------------------------------
	}
}
